package elitech.vietnam.weatherfashion.weatherservice;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;

import elitech.vietnam.weatherfashion.ultilities.JSONParser;

class WunderGroundWeatherService {

	// key chung
	// static final String WEATHER_API_URL =
	// "http://api.wunderground.com/api/431c96428597b3ce/hourly/q/";
	
	// key rieng
	static final String WEATHER_API_URL = "http://api.wunderground.com/api/59490a528bc054e9/hourly/q/";

	public ArrayList<String> getHourlyTemprature(String lat, String lon) {

		ArrayList<String> result = new ArrayList<String>();
		try {

			String urlStr = WEATHER_API_URL + lat + "," + lon + ".json";
			// Creating JSON Parser instance
			JSONParser jParser = new JSONParser();

			Log.e("GPS-Weather", "Start jParser.getJSONFromUrl(urlStr);");
			// getting JSON string from URL
			JSONObject json = jParser.getJSONFromUrl(urlStr);
			Log.e("GPS-Weather", "Stop jParser.getJSONFromUrl(urlStr);");

			Log.e("GPS-Weather", "Start json.getJSONArray(hourly_forecast)");
			JSONArray hourly_forecast = json.getJSONArray("hourly_forecast");
			Log.e("GPS-Weather", "Stop json.getJSONArray(hourly_forecast)");

			Log.e("GPS-Weather", "Start looping through All days");
			// looping through All days
			for (int i = 0; i < 24; i++) {

				JSONObject hour_temp = hourly_forecast.getJSONObject(i)
						.getJSONObject("temp");
				// String temp_F = hour_temp.getString("english");
				String temp_C = hour_temp.getString("metric");
				// JSONObject hour = hourly_forecast.getJSONObject(i)
				// .getJSONObject("FCTTIME");
				// String hourStr = hour.getString("hour");

				result.add(temp_C);
			}

			Log.e("GPS-Weather", "Stop looping through All days");

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result = new ArrayList<String>();
		}
		return result;
	}

	public ArrayList<String> getHourlyTemprature(String cityName) {

		ArrayList<String> result = new ArrayList<String>();
		try {

			String urlStr = WEATHER_API_URL + cityName + ".json";
			// Creating JSON Parser instance
			JSONParser jParser = new JSONParser();

			Log.e("GPS-Weather", "Start jParser.getJSONFromUrl(urlStr);");
			// getting JSON string from URL
			JSONObject json = jParser.getJSONFromUrl(urlStr);
			Log.e("GPS-Weather", "Stop jParser.getJSONFromUrl(urlStr);");

			Log.e("GPS-Weather", "Start json.getJSONArray(hourly_forecast)");
			JSONArray hourly_forecast = json.getJSONArray("hourly_forecast");
			Log.e("GPS-Weather", "Stop json.getJSONArray(hourly_forecast)");

			Log.e("GPS-Weather", "Start looping through All days");
			// looping through All days
			for (int i = 0; i < 24; i++) {

				JSONObject hour_temp = hourly_forecast.getJSONObject(i)
						.getJSONObject("temp");
				// String temp_F = hour_temp.getString("english");
				String temp_C = hour_temp.getString("metric");
				// JSONObject hour = hourly_forecast.getJSONObject(i)
				// .getJSONObject("FCTTIME");
				// String hourStr = hour.getString("hour");

				result.add(temp_C);
			}

			Log.e("GPS-Weather", "Stop looping through All days");

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result = new ArrayList<String>();
		}
		return result;
	}

}
